package com.ruoyi.oto.controller;

import cn.hutool.core.lang.RegexPool;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.MemberUser;
import com.ruoyi.oto.domain.dto.SetUserDTO;
import com.ruoyi.oto.domain.vo.MemberInfoVO;
import com.ruoyi.oto.service.MemberUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;


/**
 * 小程序-会员控制器
 */
@RestController
@RequestMapping("/api/v2/member")
public class MemberApiController extends BaseController {

    /**
     * 会员服务
     */
    @Autowired
    private MemberUserService memberUserService;

    /**
     * 获取会员信息
     * @return MemberInfoVO
     */
    @GetMapping("getMemberInfo")
    public AjaxResult<MemberInfoVO> getMemberInfo() {
        MemberUser member = this.getLoginUser().getMember();
        MemberInfoVO memberInfoVO =  memberUserService.getMemberInfo(member);
        return AjaxResult.success(memberInfoVO);
    }

    /**
     * 点击登录，修改会员资料
     * @return
     */
    @PostMapping("setMemberInfo")
    public AjaxResult<MemberInfoVO> setMemberInfo(@Validated @RequestBody SetUserDTO setUserDTO) {
        MemberUser member = this.getLoginUser().getMember();
        member = memberUserService.getById(member.getId());
        MemberInfoVO memberInfoVO =   memberUserService.setMemberInfo(member,setUserDTO);
        return AjaxResult.success(memberInfoVO);
    }

    /**
     * 绑定激活码
     * @param regCode 激活码
     * @return
     */
    @GetMapping("bindRegCode/{regCode}")
    public AjaxResult<Long> bindRegCode(@PathVariable String regCode) {
        MemberUser member = this.getLoginUser().getMember();
        Long id = memberUserService.bindRegCode(member,regCode);
        return AjaxResult.success(id);
    }

}
